try:
    from torch.utils.tensorboard import SummaryWriter
    TENSORBOARD_AVAILABLE = True
except ImportError:
    TENSORBOARD_AVAILABLE = False
    print("TensorBoard is not available. Install it using 'pip install tensorboard' for logging.")

from .base_logger import BaseLoggerPlugin

class TensorBoardLoggerPlugin(BaseLoggerPlugin):
    """TensorBoard日志记录插件"""
    
    def __init__(self, log_dir):
        if not TENSORBOARD_AVAILABLE:
            raise ImportError("TensorBoard is not available. Install it using 'pip install tensorboard'")
            
        self.writer = SummaryWriter(log_dir=log_dir)
        
    def log_scalar(self, tag, value, step):
        """记录标量值到TensorBoard"""
        self.writer.add_scalar(tag, value, step)
        
    def close(self):
        """关闭TensorBoard写入器"""
        self.writer.close()